package com.ezcolour.ticket.admin.controller;

import com.ezcolour.ticket.base.BaseController;
import com.ezcolour.ticket.common.log.LogManager;
import com.ezcolour.ticket.common.log.factory.LogTaskFactory;
import com.ezcolour.ticket.common.shiro.ShiroKit;
import com.ezcolour.ticket.common.shiro.ShiroUser;
import com.ezcolour.ticket.common.util.KaptchaUtil;
import com.ezcolour.ticket.model.User;
import com.ezcolour.ticket.service.UserService;
import com.ezcolour.ticket.util.ToolUtil;
import com.google.code.kaptcha.Constants;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ezcolour.ticket.bean.HttpKit.getIp;

@Controller
@RequestMapping("/admin")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 跳转到个人中心页面
     */
    @RequestMapping(value = "/center", method = RequestMethod.GET)
    public String welcome(Model model) {
        Long id = ShiroKit.getUser().getId();
        User user = userService.findById(id);
        model.addAttribute("user", user);
        return  "/admin/center";
        
    }

    /**
     * 修改管理员信息
     */
    @ResponseBody
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public String update(User user) {
        System.out.println(user);
        Long id = ShiroKit.getUser().getId();
        User loginUser = userService.findById(id);
        if(user.getId().equals(id)){
            User newUser = (User)ToolUtil.check(loginUser, user);
            userService.save(newUser);
            System.out.println("最终"+newUser);
        }
        return REDIRECT + ADMINPREFIX+ "/center";

    }
}
